package com.so.np2.algs;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class GraphicResults {
 
	List<HitResult> resultados;
	int q1;
	int q2;

	ChartPanel chartPanel;

	public GraphicResults(final List<HitResult> resultados, int q1, int q2) {
		this.resultados = resultados;
		this.q1 = q1;
		this.q2 = q2;

		final CategoryDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(600, 600));
	}

	private CategoryDataset createDataset() {

		final String[] series = new String[MainFrame.NUMERO_ALGORITMOS];
		
		series[0] = FIFO.getName();
		series[1] = Random.getName();
		series[2] = LFU.getName();
		series[3] = MFU.getName();
		series[4] = MRU.getName();
		series[5] = NUR.getName();
		series[6] = SecondChance.getName();
		series[7] = Best.getName();
		
		
		

		int varianciaFrames = (q2 - q1) + 1;
		final String[] numFrames = new String[varianciaFrames];

		int qAuxiliar = q1;
		for (int i = 0; i < numFrames.length; i++) {
			numFrames[i] = String.valueOf(qAuxiliar);
			qAuxiliar++;
		}

		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		HitResult resultado = null;
		for (int i = 0; i < series.length; i++) {
			for (int j = 0; j < numFrames.length; j++) {
				resultado = resultados.get(j);
				int acertos = resultado.getResult(series[i]);

				dataset.addValue(acertos, series[i], numFrames[j]);
			}
		}

		return dataset;
	}

	private JFreeChart createChart(final CategoryDataset dataset) {

		final JFreeChart chart = ChartFactory.createLineChart( "Q1 = " + q1 + " -   Q2 = " + q2
				, "Frames Quantity", "Hits Quantity", dataset, PlotOrientation.VERTICAL,
				true, false, false);
		
		chart.setBackgroundPaint(Color.yellow);

		final CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.black);
		plot.setRangeGridlinePaint(Color.red);

		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setAutoRangeIncludesZero(false);

		final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();

		BasicStroke basicStroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND,
				BasicStroke.JOIN_MITER, 3.0f, new float[] { 100.0f, 0.0f }, 0.0f);

		for (int i = 0; i < MainFrame.NUMERO_ALGORITMOS; i++) {
			renderer.setSeriesStroke(i, basicStroke);
		}

		return chart;
	}
}
